#ifndef __INC_QG_TEXMOTION_H__
#define __INC_QG_TEXMOTION_H__

#ifdef _MSC_VER
#pragma once
#endif

//////////////////////////////////////////////////////////////////////////
// motion texture service
class KAPI qbServAnimTexture : public qbAnimTexture
{
	KCH_BASE(qbServAnimTexture, qbAnimTexture);

public:
	typedef tpl::vector<kuintptr>	UintPtrArray;

protected:
	kStream*			_mt_stm;			// streamed stream
	qgTexture*			_mt_tex;			// output texture

	kuint				_mt_total;			// total length
	kuint				_mt_count;			// frame count
	kuint				_mt_fps : 16;		// frame per second
	kuint				_mt_bpp : 16;		// image byte per pixel

	float				_mt_fbv;			// frame bias value
	float				_mt_len;			// play length
	float				_mt_fixed;			// play fixed length
	tpl::Point			_mt_range;			// loop range
	bool				_mt_loop;			// loop
	bool				_mt_pause;			// play flag

	float				_mt_adv;			// play advance
	kuint				_mt_index;			// play index
	kuint				_mt_next;			// index for next play

	kint*				_mt_sizes;			// image size list
	kint*				_mt_locs;			// image location list
	kbyte*				_mt_data;			// read buffer

	qgImage*			_mt_ucmimg;			// 
	UintPtrArray		_mt_preimgs;		//

protected:
	qbServAnimTexture() {};
	K_VPL ~qbServAnimTexture() {};

protected:
	void _ClearStreams();
	bool _ParseMotion(kStream* stm, bool preload);
	bool _UpdateStreamImage(kuint image);
	bool _UpdateImage(kuint image);
	bool _UpdateNextImage();
	bool _UpdateAdvance(float adv);
	void _ResetLoop();

public:
	K_OVR kint GetCount() { return _mt_count; }
	K_OVR kint GetFps() { return _mt_fps; }
	K_OVR kint GetIndex() { return _mt_index; }
	K_OVR float GetLength() { return _mt_len; }
	K_OVR float GetAdvance() { return _mt_adv; }
	K_OVR bool IsLoop() { return _mt_loop; }
	K_OVR bool IsPlay() { return _mt_pause ? false : _mt_loop ? true : _mt_adv < _mt_len; }

	K_OVR void SetFixedLength(float len);
	K_OVR void SetFps(kint v);
	K_OVR void SetFrameRange(kint min, kint max);
	K_OVR void SetPosition(float pos);
	K_OVR void SetPlayMode(bool isplay);
	K_OVR void TurnLoop(bool isloop);
	K_OVR void Reset();

	//
	K_OVR bool ManualMotionUpdate(float adv, bool& res, kuintptr& ptr);
	//K_OVR bool MotionUpdate(float adv);
	K_OVR qgImage* MotionImage(kint index);
};

#endif	// __INC_QG_TEXMOTION_H__
